package com.wlyuan.open.scanner.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wlyuan.open.scanner.dao.entity.BalanceBillDetailsDO;
import com.wlyuan.open.scanner.domain.finance.BillCost;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author yuanjie
 * @since 2022-11-08
 */
public interface BalanceBillDetailsMapper extends BaseMapper<BalanceBillDetailsDO> {

    String ORDER_COUNT_SQL = " select count(distinct Detail_OrderID) from  Balance_BillDetails where  Detail_DocID= #{id} " +
            "and Detail_Invalid=0";

    String ORDER_CLOSE_COUNT_SQL = "select count(distinct Detail_OrderID) from  Balance_BillDetails where  Detail_DocID= #{id} " +
            "and Detail_UpdateTime is not null";

    String SUM_COST_SQL = "select d.Detail_DocID as financeId, COUNT(d.Detail_OrderID) as orderCount, SUM(d.Detail_Weight) as totalWeight,SUM(d.Detail_Volume) as totalVolume, SUM(d.Detail_Amount) as totalQuantity,SUM(a.Index_GoodsValue) as totalValue, " +
            "SUM(b.Cache_TotalCost) as totalCost,SUM(b.Index_Payment) as payment,SUM(b.Index_Payable) as payable,SUM(b.Index_Paysub) as paysub, " +
            "SUM(b.Cache_LessCost) as lessCost,SUM(b.Cache_FullCost) as fullCost,SUM(b.Cache_PickCost) as pickCost,SUM(b.Cache_DeliveryCost) as deliveryCost,SUM(b.Cache_OnLoadCost) as onLoadCost, " +
            "SUM(b.Cache_OffLoadCost) as offLoadCost,SUM(b.Cache_InsuranceCost) as insuranceCost,SUM(b.Cache_TaxCost) as taxCost,SUM(b.Cache_AdditionCost) as additionCost,SUM(b.Cache_MinCost) as minCost " +
            "from Balance_BillDetails d inner join TMS_OrderIndexCaches b on d.Detail_OrderID= b.Caches_OrderID " +
            "LEFT JOIN TMS_OrderIndex a on d.Detail_OrderID = a.Index_ID ";

    String ORDER_SQL = "SELECT DISTINCT a.Index_ID as detailOrderid,a.Index_PactCode as detailPactcode " +
            "FROM TMS_OrderIndex a inner join TMS_OrderIndexCaches b on a.Index_ID=b.Caches_OrderID " +
            "left join Balance_BillDetails d on a.Index_ID = d.Detail_OrderID " +
            " where d.Detail_DocID = #{billId} and d.Detail_Invalid=0";

    String ORDER_CLOSE_SQL = "SELECT DISTINCT a.Index_ID as detailOrderid,a.Index_PactCode as detailPactcode " +
            "FROM TMS_OrderIndex a inner join TMS_OrderIndexCaches b on a.Index_ID=b.Caches_OrderID " +
            "left join Balance_BillDetails d on a.Index_ID = d.Detail_OrderID " +
            "where d.Detail_DocID = #{billId} and d.Detail_UpdateTime is not null";

    /**
     * 获取列表
     *
     * @param billId
     * @return
     */
    @Select(ORDER_SQL)
    List<BalanceBillDetailsDO> getList(Long billId);

    /**
     * 获取close列表
     *
     * @param billId
     * @return
     */
    @Select(ORDER_CLOSE_SQL)
    List<BalanceBillDetailsDO> getCloseList(Long billId);


    /**
     * 根据对账单id获取对账单内得订单数,对账单关闭的数量
     *
     * @param id
     * @return
     */
    @Select(ORDER_CLOSE_COUNT_SQL)
    Long getOrderCountByBillIndexIdClose(Long id);

    /**
     * 根据对账单id获取对账单内得订单数
     *
     * @param id
     * @return
     */
    @Select(ORDER_COUNT_SQL)
    Long getOrderCountByBillIndexId(Long id);

    /**
     * 获取对账单总费用信息
     *
     * @return
     */
    @Select(SUM_COST_SQL + " where d.Detail_UpdateTime is not null GROUP BY d.Detail_DocID ")
    List<BillCost> getCostClose();

    /**
     * 获取对账单总费用信息
     *
     * @return
     */
    @Select(SUM_COST_SQL + " where d.Detail_Invalid=0 GROUP BY d.Detail_DocID ")
    List<BillCost> getCost();

}
